<cfcomponent name="SvnTreeActions">

	<cffunction name="init" returnType="any" access="public" output="false" hint="I rock">
		<cfargument name="svnService" required="true">		
		<cfargument name="json" required="true">		
		<cfset variables.instance.svnService = arguments.svnService>
		<cfset variables.instance.json = arguments.json>
	</cffunction>

	<cffunction name="getSvnService" returnType="any" access="public" output="false" hint="I rock">
		<cfreturn variables.instance.svnService>
	</cffunction>

	<cffunction name="getJson" returnType="any" access="public" output="false" hint="I rock">
		<cfreturn variables.instance.json>
	</cffunction>

<cffunction name="getParents" output="false" access="private">
	<cfargument name="tree_ID" required="true">	
	<cfset var daParents = getSvnService().List("/")>
	<cfsavecontent variable="parentDojoDivs">
	<cfoutput query="daParents">
		<cfif kind eq "dir">
			<cfset isFolder = true>
		<cfelse>
			<cfset isFolder = false>
		</cfif>
		<div dojoType="TreeNodeV3" class="wert" widgetId="/#name#" objectId="treenode" object="href:/#name#:#revision#" title="#name#" isFolder="#isFolder#">
		</div>
	</cfoutput>
	<cfif daParents.recordcount eq 0>
	<cfoutput>
	<div dojoType="TreeNodeV3" widgetId="0" objectId="treenode" title="Initial Item (not saved)" isFolder="true">
	</div>
	</cfoutput>
	</cfif>
	</cfsavecontent>
	<cfreturn parentDojoDivs>
</cffunction>

<cffunction name="setNode" output="false">
	<cfargument name="nodeID" required="true">
	<cfargument name="objectID" default="">
	<cfargument name="title" default="">
	<cfargument name="parentID" default="">
	<cfargument name="index" default="">
	<cfargument name="treeID" default="">
	<cfset var returnMsg = "">
	<cfset treeID = replacenocase(treeID,"tree","","all")>
	<cftry>
	<cfif nodeID eq 0>
		<cfreturn "({title:""#title#"",widgetId:""#title#""})">
	<cfelse>
		<cfquery name="updateNode" datasource="#request.dsource#" result="daRes">
			UPDATE dojotrees SET
			 <cfif treeID gt "">
			 	tree_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#treeID#">,			 
			 </cfif>
			 <cfif objectID gt "" AND objectID eq "tree">
			 	parent_tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="" null="true">,
			 <cfelseif objectID gt "" AND objectID neq "tree">
			 	parent_tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#parentID#">,
			 </cfif>
			 <cfif index gt "">
			 	tree_item_order = <cfqueryparam cfsqltype="cf_sql_integer" value="#index#">,
			 </cfif>
			 <cfif title gt "">
			 	tree_item_display = <cfqueryparam cfsqltype="cf_sql_varchar" value="#title#">,
			 </cfif>
			 tree_item_ID = #nodeID#
			 WHERE tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#nodeID#">
		</cfquery>
		<cfsavecontent variable="daDumpq">
			<cfdump var="#daRes#">
		</cfsavecontent>
		<cfset session.funstuff = session.funstuff & daDumpq>
	</cfif>
	<cfcatch>
	  <cfset wee = addServerDebug(cfcatch)>
	  <cfset returnMsg = "error:""error setting node""">
	</cfcatch>
	</cftry>
	<cfif returnMsg eq "">
		<cfreturn "({})">
	<cfelse>
		<cfreturn "(#returnMsg#)">
	</cfif>
</cffunction>

<cffunction name="deleteNode" output="false">
	<cfargument name="nodeID">
	<cfquery name="check4kids" datasource="#request.dsource#">
	  SELECT * FROM dojotrees
	  WHERE parent_tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#nodeID#">
	</cfquery>
	<cfif check4kids.recordcount eq 0>
		<cfquery name="getNewNode" datasource="#request.dsource#">
		  DELETE FROM dojotrees
		  WHERE tree_item_ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#nodeID#">
		</cfquery>
		<cfreturn true>
	<cfelse>
		<cfreturn false>
	</cfif>
</cffunction>

<!--- DATA RETURN STUFF --->
<cfscript>
function getChildren(tree_ID,parentID) {
	var theKidsQry = getSvnService().List(parentID);
	var kids = "";
	var kidInfo = "";
	var revInfo = "";
	for (x = 1; x lte theKidsQry.recordcount; x=x+1) {
		kidInfo = "";
		kidInfo = listAppend(kidInfo,'widgetId:"#theKidsQry["url"][x]#"');
		revInfo = '<span class=''treeRevInfo''>#theKidsQry["revision"][x]# #theKidsQry["author"][x]#</span>';
		//kidInfo = listAppend(kidInfo,'objectId:"TreeNodeV3"');
		if (lcase(theKidsQry["kind"][x]) eq "dir") {
			kidInfo = listAppend(kidInfo,'title:"<strong>#theKidsQry["name"][x]#/</strong> #revInfo#"');
			kidInfo = listAppend(kidInfo,'isFolder:true');
			kidInfo = listAppend(kidInfo,'object:{href: "&resourceUrl=#theKidsQry["url"][x]#/:#theKidsQry["revision"][x]#",title:"Revision:#theKidsQry["revision"][x]# - Modified:#dateformat(theKidsQry["date"][x],"mm/dd/yy")# - Author:#theKidsQry["author"][x]#"}');
		} 
		else {
			kidInfo = listAppend(kidInfo,'title:"#theKidsQry["name"][x]# #revInfo#"');
			kidInfo = listAppend(kidInfo,'object:{href: "&resourceUrl=#theKidsQry["url"][x]#:#theKidsQry["revision"][x]#",title:"Revision:#theKidsQry["revision"][x]# - Modified:#dateformat(theKidsQry["date"][x],"mm/dd/yy")# - Author:#theKidsQry["author"][x]#"}');
		}
		kidInfo = listAppend(kidInfo,'index:#val(0)#');
		kidInfo = "{#kidInfo#}";
		kids = listAppend(kids,kidInfo);
	}
	return "([#kids#])";	
}

function editLabelSave(editLabelData) {
	var moveWidget = editLabelData["node"]["widgetId"];
	var newTitle = editLabelData["newContent"];
	return setNode(nodeID=moveWidget,title=newTitle);
	//return "({title:""#newTitle#""})";
}

function createChild(childdata) {
	var newParentID = childdata["parent"]["widgetId"];
	// var sugTitle = childdata["data"]["suggestedTitle"];
	var newTitle = childdata["data"]["title"];
	var newParentTreeID = childdata["tree"]["widgetId"];
	newParentTreeID = replace(newParentTreeID,"tree","","all");
	return setNode(nodeID=0,title=newTitle,objectID="TreeNodeV3",parentID=newParentID,treeID=newParentTreeID);
}

function removeNode(nodedata) {
	var widgetId = nodedata["node"]["widgetId"];
	return deleteNode(widgetId);
}

function moveNode(movedata) {
	var moveWidget = movedata["child"]["widgetId"];
	var newIndex = movedata["newIndex"];
	var newParentID = movedata["newParent"]["widgetId"];
	var newObjectID = movedata["newParent"]["objectId"];
	var newParentTreeID = movedata["newParentTree"]["widgetId"];
	return setNode(nodeID=moveWidget,objectID=newObjectID,index=newIndex,parentID=newParentID,treeID=newParentTreeID);
}

function runAction(action,data) {
	var daData = "";
	//setServerDebug();
	switch ("#LCase(action)#") {
		case "getChildren":
			daData = getJson().jsondecode(data);
			daChilln = getChildren(daData["tree"]["widgetId"],daData["node"]["widgetId"]);
	    //dumpvar(daChilln,true);
//			addServerDebug(daChilln);
	    return daChilln;
		case "createChild":
			daData = getJson().jsondecode(data);
			addServerDebug(daData);
			return createChild(daData);
		case "removeNode":
			daData = getJson().jsondecode(data);
			addServerDebug('Removed Node');
			return removeNode(daData);
		case "move":
			daData = getJson().jsondecode(data);
			addServerDebug('Move');
			return moveNode(daData);
		case "editLabelSave":
			daData = getJson().jsondecode(data);
			editLabelSaveRslt = editLabelSave(daData);
			addServerDebug(editLabelSaveRslt);
			return editLabelSaveRslt;
		default:
	  	return;
	}
}
</cfscript>

</cfcomponent>
